﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using Emerson.Storage.Service.Interface;
using Emerson.Storage.Entities;
using System.Web.Services;
using System.Web.Script.Services;
using System.Collections;
using System.ComponentModel;

namespace WorkstationDashboard
{
    public partial class ContentRoot : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BuildTreeView();
                SetRootInfo();
            }
        }

        private void SetRootInfo()
        {
            IApplicationService appService = ServiceHelper.GetService<IApplicationService>();
            ApplicationInfo ai = appService.GetApplicationInfo();
            databaseLabel.Text = ai.DatabaseName;
            ipAddressLabel.Text = ai.ServerAddress;
            hostNameLabel.Text = ai.ServerName;
            rootName.Text = ai.ServerName;

            IWorkstationService wsService = ServiceHelper.GetService<IWorkstationService>();
            WorkstationRoot root = wsService.GetWorkstationRoot();
            int okCount = 0;
            int disconnectedCount = 0;

            foreach (Group g in root.Groups)
            {
                foreach (Workstation w in g.Workstation)
                {
                    if (w.IsConnected)
                        okCount++;
                    else
                        disconnectedCount++;
                }
            }

            okWorkstationCount.Text = " " + okCount.ToString();
            disconnectedWorkstationCount.Text = " " + disconnectedCount.ToString();
        }

        private void BuildTreeView()
        {
            IWorkstationService wsService = ServiceHelper.GetService<IWorkstationService>();

            WorkstationRoot wkRoot = wsService.GetWorkstationRoot();
            TreeNode treeRoot = Utility.BuildTreeFromWorkstationRoot(wkRoot);
            Master.WorkstationTreeView.Nodes.Clear();
            Master.WorkstationTreeView.Nodes.Add(treeRoot);
            treeRoot.Select();
        }

        [WebMethod]
        [ScriptMethod]
        public static string AddGroup(string groupName, string groupNote)
        {
            Group newGroup = new Group();
            newGroup.Name = groupName.Trim();
            newGroup.Note = groupNote.Trim();

            IWorkstationService wsService = ServiceHelper.GetService<IWorkstationService>();
            wsService.AddGroup(newGroup);

            return "Group " + groupName + " is added";
        }

        void addGroup_Click(object sender, EventArgs e)
        {
            string name = "11";
            string note = "22";
            if (string.IsNullOrEmpty(name))
                return;
            Group g = new Group { Name = name, Note = note };

            IWorkstationService wsService = ServiceHelper.GetService<IWorkstationService>();
            wsService.AddGroup(g);

            WorkstationRoot wsRoot = wsService.GetWorkstationRoot();
            Session[SessionKeys.WorkstationRoot] = wsRoot;
            TreeNode treeRoot = Utility.BuildTreeFromWorkstationRoot(wsRoot);
            Master.WorkstationTreeView.Nodes.Clear();
            Master.WorkstationTreeView.Nodes.Add(treeRoot);
        }
    }
}